﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class RepairModels : _RepairModels
    {
        public static RepairModels AddModel(string name, int brandID, int userID)
        {
            try
            {
                RepairModels r = new RepairModels();
                r.AddNew();
                r.ModelName = name;
                r.LastModifiedByUser = userID;
                r.BrandID = brandID;
                r.Deleted = false;
                r.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);

                r.Save();
                return r;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "AddModel", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairModels GetModelsByBrandID(int brandID)
        {
            try
            {
                RepairModels r = new RepairModels();
                r.Where.BrandID.Value = brandID;
                r.Where.Deleted.Value = false;
                r.Query.Load();

                return r;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "GetModelsByBrandID", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairModels GetAllActiveModels()
        {
            try
            {
                RepairModels r = new RepairModels();
                r.Where.Deleted.Value = false;
                r.Where.BranchID.Value = Branches.CurrentBranchID;
                r.Query.Load();

                return r;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "GetAllActiveModels", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairModels GetAllActiveModels(int branchID)
        {
            try
            {
                RepairModels r = new RepairModels();
                r.Where.Deleted.Value = false;
                r.Where.BranchID.Value = branchID;
                r.Query.Load();

                return r;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "GetAllActiveModels", ex.StackTrace);
                throw ex;
            }
        }

        public static Boolean IsModelNameExists(string name)
        {
            try
            {
                RepairModels r = new RepairModels();
                r.Where.ModelName.Value = name;
                r.Where.Deleted.Value = false;
                r.Query.Top = 1;
                r.Query.Load();

                return r.RowCount > 0;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "AddModel", ex.StackTrace);
                throw ex;
            }
        }

        public static void DeleteModel(int modelID, int userID)
        {
            try
            {
                RepairModels r = new RepairModels();
                r.LoadByPrimaryKey(modelID);

                r.Deleted = false;
                r.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                r.LastModifiedByUser = userID;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModels.cs", "AddModel", ex.StackTrace);
                throw ex;
            }
        }


    }

}
